/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.mushroom.website.daoImpl;


import cz.muni.fi.pa165.mushroom.website.dao.VisitDao;
import cz.muni.fi.pa165.mushroom.website.entities.Forest;
import cz.muni.fi.pa165.mushroom.website.entities.Gatherer;
import cz.muni.fi.pa165.mushroom.website.entities.Mushroom;
import cz.muni.fi.pa165.mushroom.website.entities.Visit;
import java.util.ArrayList;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

/**
 * Implementation of VisitDao interface
 *
 * @author Roman Jakubco
 */
@Repository
public class VisitDaoImpl implements VisitDao {
    @PersistenceContext
    private EntityManager em;


    public void createVisit(Visit visit) {
        if (visit == null) throw new IllegalArgumentException("Visit is null");
        if (visit.getId() != null) throw new IllegalArgumentException("Visit ID is already set");
        if (visit.getForest() == null) throw new IllegalArgumentException("Visit has no forest");
        if (visit.getGatherer() == null) throw new IllegalArgumentException("Visit has no gatherer");
        List<Mushroom> temp = new ArrayList<>();
        if(visit.getMushrooms() != null){
           for(Mushroom m : visit.getMushrooms()){
                temp.add(em.merge(m));
            }
            visit.setMushrooms(temp); 
        }
        
        em.persist(visit);
    }

    public void updateVisit(Visit visit) {
        if (visit == null) throw new IllegalArgumentException("Visit is null");
        if (visit.getId() == null) throw new IllegalArgumentException("Visit has no ID");
        if (visit.getForest() == null) throw new IllegalArgumentException("Visit has no forest");
        if (visit.getGatherer() == null) throw new IllegalArgumentException("Visit has no gatherer");

        em.merge(visit);
    }

    public void deleteVisit(Visit visit) {
        if (visit == null) throw new IllegalArgumentException("Visit is null");
        if (visit.getId() == null) throw new IllegalArgumentException("Visit has no ID");
        Visit remove = em.getReference(Visit.class, visit.getId());
        em.remove(remove);         
    }

    public Visit findVisit(Long id) {
        if (id == null) throw new IllegalArgumentException("Visit ID is null");

        return em.find(Visit.class, id);
    }

    public List<Visit> listVisits() {
        TypedQuery<Visit> query = em.createQuery("SELECT v FROM Visit v", Visit.class);
        return query.getResultList();
    }

    public List<Visit> listVisitsByForest(Forest forest) {
        TypedQuery<Visit> query = em.createQuery("SELECT v FROM Visit v WHERE v.forest=?1", Visit.class).setParameter(1, forest);
        return query.getResultList();
    }

    public List<Visit> listVisitsByGatherer(Gatherer gatherer) {
        TypedQuery<Visit> query = em.createQuery("SELECT v FROM Visit v WHERE v.gatherer=?1", Visit.class).setParameter(1, gatherer);
        return query.getResultList();
    }

}
